<?php

namespace App\Admin\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use \App\Models\Activity;
use \App\Models\Coupon;

class ActivitiesController extends AdminController
{
    protected $title = '活动管理';
    
    protected function grid()
    {
        $grid = new Grid(new Activity());

	    $grid->disableExport();
	
	    $grid->column('id', 'ID');
	    $grid->column('title', '活动标题');
	     $grid->column('quantity', '每人每天可兑换数量');
	  
	    $grid->column('image', '图片')->lightbox(['width' => 50, 'height' => 50]);

	    $grid->column('acticity_started_at', '活动开始时间')->display(function () {
		    return $this->acticity_started_at;
	    });
	    $grid->column('acticity_ended_at', '活动结束时间')->display(function () {
		    return $this->acticity_ended_at;
	    });
	   
	    $grid->column('created_at', '创建时间')->display(function () {
		    return $this->created_at->toDatetimeString();
	    });
	
	    $grid->disableRowSelector();

        return $grid;
    }
	
	 protected function detail($id)
    {
        $show = new Show(Activity::findOrFail($id));
        return $show;
    }
	
    protected function form()
    {
        $form = new Form(new Activity());

	    $form->text('title', '活动标题')->rules('required');
	    $form->multipleSelect('coupons', '优惠卷')->options(Coupon::all()->pluck('name', 'id'));
	    $form->number('quantity', '每人可兑换数量')->rules('numeric');
	    $form->textarea('desc', '活动详情')->rules('required');
	    $form->image('image', '图片')->uniqueName()->help('NOTE：保证清晰的前提下尽可能压缩体积');
	    $form->dateTimeRange('acticity_started_at', 'acticity_ended_at', '活动时间范围');
	    
        return $form;
    }
}
